package com.wg.net.operations.base;


import cn.hutool.core.date.TimeInterval;
import com.wg.core.io.IBinaryReader;
import com.wg.core.io.IBinaryWriter;
import com.wg.core.io.IBinaryWritable;

public class OperationRequest {

    private final TimeInterval handleInterval = new TimeInterval(true);

    /**
     * 操作编号
     */
    protected short opCode;

    /**
     * bytes
     */
    private byte[] buffer;

    public OperationRequest() {
    }

    public OperationRequest(short opCode) {
        this.opCode = opCode;
    }

    public OperationRequest(short opCode, byte[] buffer) {
        this.opCode = opCode;
        this.buffer = buffer;
    }

    public OperationRequest(short opCode, IBinaryWritable contract) {
        this(opCode, contract.toBytes());
    }

    public short getOpCode() {
        return opCode;
    }

    public void setOpCode(short opCode) {
        this.opCode = opCode;
    }

    public byte[] getBuffer() {
        return buffer;
    }

    public void setBuffer(byte[] buffer) {
        this.buffer = buffer;
    }

    public TimeInterval getHandleInterval() {
        return handleInterval;
    }

    public void read(IBinaryReader reader) {
        opCode = reader.readShort();
        buffer = reader.readRemainingBytes();
    }

    public void write(IBinaryWriter writer) {
        writer.writeShort(opCode);
        writer.writeBytes(buffer);
    }

    @Override
    public String toString() {
        return "requestCode=" + opCode;
    }
}
